﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System . Text ;
using Teenusekiht ;

public partial class UserControls_CommControl : System.Web.UI.UserControl
{

    public byte Sets {get;set;}
    public bool IsInp {get;set;}
    private bool IsByEvent = false ;
    private bool IsBySet = false ;

    private int? kId ;    //kohvik või muu - nullable, kui special adm. page
    private int? uId ;    //aktiivne kasutja
    private string uNa ;  //aktiivse kasutaja username
    private string uDi ;  //aktiivse kasutaja display name

    private string rTe ;
    private string rLi ;

    private int? cId = null ;  // kommentaari id
    private int? cUId = null ; // kommenteerija id
    private string uName ;
    private string dText ;//TEKSTI PIKKUSTE KONTROLLID SIIN VÕI MUJAL (valid?)!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    private DateTime? cDate ;
    private DateTime? mDate ;
    private DateTime? dDate ;
    private ushort dValue ;
    private bool dHidden ;
    private byte cState ;

    private byte action = SETS . COMMACTINONE ;
    private bool isSure = false ;

    private CommServ cs ;
    private bool result = false ;
    private GenCom returned = null ;
    private RatCom [] values = null ;

    protected void Page_Init ( object sender , EventArgs e )
    {
    }

    protected void Page_Load ( object sender , EventArgs e )
    {
        cs = new CommServ ( Sets ) ;
    }

    public void SetVals ( int? kId , int? uId , string uNa , string uDi )
    {
        this . kId = kId ;
        this . uId = uId ;
        this . uNa = uNa ;
        this . uDi = uDi ;
    }

    public void SetFields ( GenCom gc , RatCom [] rcs )
    {
        this . cId = gc . cId ;
        this . cUId = gc . cUId ;
        this . uName = gc . uName ;
        this . dText = gc . dText ;
        this . cDate = gc . cDate ;
        this . mDate = gc . mDate ;
        this . dDate = gc . dDate ;
        this . dValue = gc . dValue ;
        this . dHidden = gc . dHidden ;
        this . cState = gc . cState ;
        if ( CheckIfIsOwner () ) values = rcs ;
        IsBySet = true ;
    }

    public EdiCom GetFields ()
    {
        RetrieveValues () ;
        EdiCom ec = new EdiCom () ;
        ec . cUId = this . uId ;
        ec . uName = this . uName ;
        ec . dText = this . dText ;
        return ( ec ) ;
    }

    private void RetrieveValues ()
    {
        if ( tbCont . Text == "" ) dText = lbCont . Text ;
          else dText = tbCont . Text ;
        if ( IsInp ) {
            uName = ( uId == null ) ? tbUsern . Text : "" ; }
          else {
            int parsed ;
            cId = ( int . TryParse ( hfComm . Value , out parsed ) ) ? ( int? ) parsed : null ;
            if ( ( cId != null ) && ( cId < 0 ) ) cId = null ; }
    }

    private bool CheckIfIsOwner ()
    {
        return ( ( uId != null ) && ( cUId != null ) && ( cUId == uId ) ) ;
    }

    private void MakeContReadable ()
    {
        lbCont . Text = dText ;
        MakeContViewable () ;
    }

    private void MakeContEditable ()
    {
        MakeContChangeable () ;
        lbCont . Text = "" ;
    }

    private void MakeContViewable ()
    {
        lbCont . Visible = true ;
        tbCont . Visible = false ;
        tbCont . Text = "" ;
    }

    private void MakeContChangeable ()
    {
        tbCont . Text = dText ;
        tbCont . Visible = true ;
        lbCont . Visible = false ;
    }

    private void ClearText ()
    {
        tbCont . Text = "" ;
        lbCont . Text = "" ;
    }

    private void MakeRateEditable ()
    {
        ddValue . Visible = true ;
        lbValue . Visible = false ;//VANA HINNE JÄÄB ALLES LABEL'IL - SEDA UUT EI SAA KUSAGILT, KUI EI REFRESH'I ANDMEID!!!!!!!!!!!!!!
    }                              //alles hoidmine vajalik, kui muutmine katkestatakse, muutmisel võetakse uus väärtus...

    private void MakeRateReadable ()
    {
        lbValue . Visible = true ;//VANA HINNE ON ALLES LABEL'IL - SEDA UUT EI SAA KUSAGILT, KUI EI REFRESH'I ANDMEID!!!!!!!!!!!!!!
        ddValue . Visible = false ;
    }

    private void MakeUnsure ()
    {
        hfSure . Value = "0" ;
    }

    private void MakeSure ()
    {
        hfSure . Value = "1" ;
    }

    private void MakeHidden ()
    {
        hfBUsern . Value = "" + SETS . COMMACTISHOW ;
    }

    private void MakeShown ()
    {
        hfBUsern . Value = "" + SETS . COMMACTIHIDE ;
    }

    private void MakeInfoSuccess ()
    {
        lbMessage . Text = SETS . COMMINTESUCC ;
    }

    private void MakeInfoFailure ()
    {
        lbMessage . Text = SETS . COMMINTEFAIL ;
    }

    private void MakeInfoNothing ()
    {
        lbMessage . Text = "" ;
    }

    private void MakeUsernForInput ()
    {
        if ( uId == null ) {
            tbUsern . Visible = true ;
            lbUsern . Visible = false ;
            lbUsern . Text = "" ;
            tbUsern . Text = SETS . ANONYMOUS_NA ; }
          else {
            MakeUsernForRead () ; }
    }

    private void MakeUsernForOutput ()
    {
        if ( CheckIfIsOwner () ) {
            MakeUsernForButton () ; }
          else if ( cUId != null ) {
            hlUsern . Visible = true ;
            btUsern . Visible = false ;
            lbUsern . Visible = false ;
            btUsern . Text = "" ;
            lbUsern . Text = "" ;
            hlUsern . Text = uName ;
            hlUsern . NavigateUrl = SETS . IDLINKUSERPAGE + uId ; }
          else {
            MakeUsernForRead () ; }
    }

    private void MakeUsernForRead ()
    {
        lbUsern . Visible = true ;
        if ( IsInp ) {
            lbUsern . Text = uDi ;
            tbUsern . Visible = false ;
            tbUsern . Text = "" ; }
          else {
            lbUsern . Text = uName ;
            btUsern . Visible = false ;
            hlUsern . Visible = false ;
            btUsern . Text = "" ;
            hlUsern . Text = "" ; }
    }

    private void MakeUsernForButton ()
    {
        btUsern . Visible = true ;
        hlUsern . Visible = false ;
        lbUsern . Visible = false ;
        hlUsern . Text = "" ;
        lbUsern . Text = "" ;
        if ( dHidden ) btUsern . Text = SETS . COMMCONTSHOW ;
          else btUsern . Text = SETS . COMMCONTHIDE ;
    }

    private void MakeButLeftReportCon ()
    {
        btLeft . Text = SETS . COMMCONTCONF ; //kinnita
        hfBLeft . Value = "" + SETS . COMMACTIREPO ;
    }

    private void MakeButRightRem ()
    {
        btRight . Text = SETS . COMMCONTREMO ;
        hfBRight . Value = "" + SETS . COMMACTIREMO ;
    }

    private void MakeButLeftCan ()
    {
        btLeft . Text = SETS . COMMCONTCANC ;
        hfBLeft . Value = "" + SETS . COMMACTIDEFA ;
    }

    private void MakeButRightCan ()
    {
        btRight . Text = SETS . COMMCONTCANC ;
        hfBRight . Value = "" + SETS . COMMACTIDEFA ;
    }

    private void MakeBsDone () //SEDA ON VAJA KASUTAJA PRÜGIKASTIS!!!!
    {
        btLeft . Visible = false ;
        btLeft . Text = "" ;
        hfBLeft . Value = "" ;
        btRight . Visible = false ;
        btRight . Text = "" ;
        hfBRight . Value = "" ;
        btUsern . Visible = false ;
        btUsern . Text = "" ;
        hfBUsern . Value = "" ;
        lbMessage . Text = SETS . COMMCONTDONE ;
    }

    private void MakeDateTime ()
    {
        StringBuilder sb = new StringBuilder ( 255 ) ;
        sb . Append ( SETS . COMMINTECREA ) ;
        sb . Append ( SETS . COMMINTEVALU ) ;
        sb . Append ( cDate . ToString () ) ;
        if ( mDate != null ) {
            sb . Append ( SETS . COMMINTESEPA ) ;
            sb . Append ( SETS . COMMINTEMODI ) ;
            sb . Append ( SETS . COMMINTEVALU ) ;
            sb . Append ( mDate . ToString () ) ; }
        if ( dDate != null ) {
            sb . Append ( SETS . COMMINTESEPA ) ;
            sb . Append ( SETS . COMMINTEDELE ) ;
            sb . Append ( SETS . COMMINTEVALU ) ;
            sb . Append ( dDate . ToString () ) ; }
        lbTime . Text = sb . ToString () ;
    }

    private void MakeStaticData ()
    {
        hfComm . Value = ( cId != null ) ? cId . ToString () : "" ;
        btLeft . Visible = true ;
        btRight . Visible = true ;
        btUsern . Visible = true ;
        lbMessage . Text = "" ;
        if ( IsInp ) {
            MakeContEditable () ;
            ClearText () ;
            MakeUsernForInput () ; }
          else {
            MakeUsernForOutput () ;
            MakeShown () ;
            MakeContReadable () ;
            MakeDateTime () ;
            switch ( Sets ) {
              case SETS . CAFECU : case SETS . FOODCU : case SETS . CAFECM : case SETS . FOODCM : case SETS . CAFECA : case SETS . FOODCA :
                lbWhat . Text = SETS . COMMINTERATE ; break ;
              case SETS . FORUMU : case SETS . FORUMM : case SETS . FORUMA :
                lbWhat . Text = SETS . COMMINTEPOST ; break ; default : break ; }
            lbValue . Text = "" + dValue ;
            ddValue . Visible = false ; }
        MakeToDefault () ;
    }

    private void MakeToDefault ()
    {
        MakeUnsure () ;
        if ( IsInp ) {
            btLeft . Text = SETS . COMMCONTPREV ;
            hfBLeft . Value = "" + SETS . COMMACTIPREV ;
            btRight . Text = SETS . COMMCONTCLEA ;
            hfBRight . Value = "" + SETS . COMMACTICLEA ;
            /*MakeContEditable () ;*/ }
          else {
            MakeContViewable () ;
            if ( CheckIfIsOwner () ) {//kas kustutamise jaoks lisada siia eraldi delete action??
                btLeft . Text = SETS . COMMCONTMODI ;//modif
                hfBLeft . Value = "" + SETS . COMMACTIMODI ;
                btRight . Text = SETS . COMMCONTDELE ;//KUI KUSTUTATUD, SIIS PEAKS OLEMA VÕIMALIK KA TAASTADA KOHE (nupud muuta ja hf luua?)!!
                hfBRight . Value = "" + SETS . COMMACTIDELE ; }
              else {
                switch ( Sets ) {
                  case SETS . CAFECU : case SETS . FOODCU : case SETS . FORUMU :
                    btLeft . Enabled = false ;
                    MakeButLeftReportCon () ;
                    btRight . Text = SETS . COMMCONTREPO ;//report
                    hfBRight . Value = "" + SETS . COMMACTIREPO ; break ;
                  case SETS . CAFECM : case SETS . FOODCM : case SETS . FORUMM :
                    btLeft . Text = SETS . COMMCONTREPO ;//report
                    hfBLeft . Value = "" + SETS . COMMACTIREPO ;
                    MakeButRightRem () ; break ;//remove
                  case SETS . CAFECA : case SETS . FOODCA : case SETS . FORUMA :
                    btLeft . Text = SETS . COMMCONTALLO ;//luba
                    hfBLeft . Value = "" + SETS . COMMACTIALLO ;
                    btRight . Text = SETS . COMMCONTDENY ;//keela
                    hfBRight . Value = "" + SETS . COMMACTIDENY ; break ; default : break ; } } }
            if ( values != null ) {
                foreach ( RatCom val in values ) {
                    ddValue . Items . Add ( new ListItem ( val . rDes , "" + val . rVal ) ) ; }
                if ( ddValue . Items . Count != 0 ) ddValue . SelectedValue = "" + dValue ; }//SEE PEAKS OLEMA HETKEL SEATUD HINNE DEFAULT'INA!!!!!!!
    }

    private void DoAction ()
    {
        switch ( action ) {
          case SETS . COMMACTIHIDE : result = cs . HideComm ( uId , cId ) ; break ;
          case SETS . COMMACTISHOW : result = cs . ShowComm ( uId , cId ) ; break ;
          default :
            isSure = ( hfSure . Value == "1" ) ? true : false ;
            if ( isSure ) {
                if ( action == SETS . COMMACTIMODI ) {
                    byte parsed ;
                    dValue = ( byte . TryParse ( ddValue . SelectedValue , out parsed ) ) ? parsed : SETS . COMMRATENONE ; }
                switch ( action ) {//lisada ka lõplik kustutamine...
                  case SETS . COMMACTIREPO : result = cs . RepoComm ( uId , cId ) ; break ;
                  case SETS . COMMACTIMODI : returned = cs . ModiComm ( uId , cId , dText , dValue ) ; break ;
                  case SETS . COMMACTIDELE : result = cs . DeleComm ( uId , cId ) ; break ;
                  case SETS . COMMACTIREST : result = cs . RestComm ( uId , cId ) ; break ;
                  case SETS . COMMACTIREMO : result = cs . RemoComm ( uId , cId ) ; break ;
                  case SETS . COMMACTIALLO : result = cs . AlloComm ( uId , cId ) ; break ;
                  case SETS . COMMACTIDENY : result = cs . DenyComm ( uId , cId ) ; break ;
                  default : result = false ; returned = null ; break ; }
                if ( ( action == SETS . COMMACTIMODI ) && ( returned != null ) ) result = true ; } break ; }
    }

    private void ShowData ()
    {
        switch ( action ) {
          case SETS . COMMACTIDEFA :
            MakeToDefault () ; break ;
          case SETS . COMMACTIPREV :
            btLeft . Text = SETS . COMMCONTREVI ;
            hfBLeft . Value = "" + SETS . COMMACTIREVI ;
            MakeContReadable () ; break ;
          case SETS . COMMACTIREVI :
            btLeft . Text = SETS . COMMCONTPREV ;
            hfBLeft . Value = "" + SETS . COMMACTIPREV ;
            MakeContEditable () ; break ;
          case SETS . COMMACTIHIDE ://kasutajale võiks tema poolt peidetud kommentaarid näha jääda tema kommentaaride vaate lehel
            if ( result ) {
                btUsern . Text = SETS . COMMCONTSHOW ;
                MakeHidden () ;
                MakeInfoSuccess () ; }
              else {
                MakeInfoFailure () ; } break ;
          case SETS . COMMACTISHOW :
            if ( result ) {
                btUsern . Text = SETS . COMMCONTHIDE ;
                MakeShown () ;
                MakeInfoSuccess () ; }
              else {
                MakeInfoFailure () ; } break ;
          default :
            if ( isSure ) {
                switch ( action ) {
                  case SETS . COMMACTIMODI ://TODO: kõigile eraldi tekstid teha?
                    if ( result ) MakeStaticData () ; break ;
                  case SETS . COMMACTIDELE ://siia vaja ka lõppliku kustutamist... (kas teha selleks eraldi delete action???????????)
                    if ( result ) MakeDateTime () ; break ;
                  case SETS . COMMACTICLEA :
                    ClearText () ; break ; default : break ; }
                if ( result && ( action != SETS . COMMACTICLEA ) ) {//modify ebaõnnestumisel peaks muutmis vorm jääma ette!!!
                    MakeToDefault () ;
                    MakeInfoSuccess () ; }
                  else if ( action != SETS . COMMACTICLEA ) {
                    MakeInfoFailure () ; }
                  else {
                    MakeInfoNothing () ; } }
              else {
                MakeInfoNothing () ;
                switch ( action ) {
                  case SETS . COMMACTIREPO :
                    btLeft . Enabled = true ;
                    MakeButLeftReportCon () ;
                    MakeButRightCan () ; break ;
                  case SETS . COMMACTIMODI :
                    MakeButLeftCan () ;
                    btRight . Text = SETS . COMMCONTMODI ;//modif
                    hfBRight . Value = "" + SETS . COMMACTIMODI ;
                    MakeContChangeable () ;
                    MakeRateEditable () ; break ;
                  case SETS . COMMACTIDELE :
                    btLeft . Text = SETS . COMMCONTDELE ;
                    hfBLeft . Value = "" + SETS . COMMACTIDELE ;
                    MakeButRightCan () ; break ;
                  case SETS . COMMACTIREST :
                    btLeft . Text = SETS . COMMCONTREST ;//restore
                    hfBLeft . Value = "" + SETS . COMMACTIREST ;
                    MakeButRightCan () ; break ;
                  case SETS . COMMACTIREMO :
                    MakeButLeftCan () ;//kas need pooled ära vahetada???
                    MakeButRightRem () ; break ;//remove
                  case SETS . COMMACTIALLO :
                    MakeButLeftCan () ;
                    btRight . Text = SETS . COMMCONTALLO ;
                    hfBRight . Value = "" + SETS . COMMACTIALLO ; break ;
                  case SETS . COMMACTIDENY :
                    btLeft . Text = SETS . COMMCONTDENY ;
                    hfBLeft . Value = "" + SETS . COMMACTIDENY ;
                    MakeButRightCan () ; break ;
                  case SETS . COMMACTICLEA :
                    btLeft . Text = SETS . COMMCONTCLEA ;
                    hfBLeft . Value = "" + SETS . COMMACTICLEA ;
                    MakeButRightCan () ; break ; default : break ; }
                MakeSure () ; } break ; }
    }

    protected void BLeft_Click ( object sender , EventArgs e )
    {
        byte parsed ;
        action = ( byte . TryParse ( hfBLeft . Value , out parsed ) ) ? parsed : SETS . COMMACTINONE ;
        RetrieveValues () ;
        DoAction () ;
        IsByEvent = true ;
    }

    protected void BRight_Click ( object sender , EventArgs e )
    {
        byte parsed ;
        action = ( byte . TryParse ( hfBRight . Value , out parsed ) ) ? parsed : SETS . COMMACTINONE ;
        RetrieveValues () ;
        DoAction () ;
        IsByEvent = true ;
    }

    protected void BUsern_Click ( object sender , EventArgs e )
    {
        byte parsed ;
        action = ( byte . TryParse ( hfBUsern . Value , out parsed ) ) ? parsed : SETS . COMMACTINONE ;
        RetrieveValues () ;
        DoAction () ;
        IsByEvent = true ;
    }

    //protected void DRev_SelChanged ( object sender , EventArgs e )
    //{

    //}

    protected void Page_PreRender ( object sender , EventArgs e )
    {
        if ( IsInp ) divTopLeft . Controls . Remove ( hlUsern ) ;
          else divTopLeft . Controls . Remove ( tbUsern ) ;
        if ( IsByEvent ) {
            ShowData () ; }
          else if ( IsBySet ) {
            MakeStaticData () ;
            /*ShowData () ;*/ }
        if ( IsInp ) {
            divMain . Controls . Remove ( hfBUsern ) ;
            divTopLeft . Controls . Remove ( btUsern ) ;
            //divTopLeft . Controls . Remove ( hlRef ) ;
            divTopRight . Controls . Remove ( lbMessage ) ;
            //divTopRight . Controls . Remove ( ddRev ) ;
            divBottomLeft . Controls . Remove ( lbTime ) ;
            divBottomRight . Controls . Remove ( lbWhat ) ;
            divBottomRight . Controls . Remove ( lbValue ) ;
            divBottomRight . Controls . Remove ( ddValue ) ; }
    }//TODO: POSTBACK'IL GRID DATA, või on is by set selle asemel?????????????????????????????????????????????!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    protected void Page_Unload ( object sender , EventArgs e )
    {
        if ( cs != null ) cs . Dispose () ;
    }

}